package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.FinanceReceivablesMapper;
import cn.tedu.erp.project.pojo.dto.FinanceReceivablesAddNewDTO;
import cn.tedu.erp.project.pojo.dto.FinanceReceivablesUpdateDTO;
import cn.tedu.erp.project.pojo.entity.FinanceReceivables;
import cn.tedu.erp.project.pojo.vo.FinanceReceivablesListVO;
import cn.tedu.erp.project.pojo.vo.FinanceReceivablesStandardVO;
import cn.tedu.erp.project.service.IFinanceReceivablesService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FinanceReceivablesServiceImpl implements IFinanceReceivablesService {
    @Autowired
    private FinanceReceivablesMapper financeReceivablesMapper;

    public FinanceReceivablesServiceImpl() {
        log.debug("创建业务类对象：FinanceReceivablesServiceImpl");
    }

    @Override
    public void addNew(FinanceReceivablesAddNewDTO financeReceivablesAddNewDTO) {
        log.debug("开始处理【添加应收帐】的业务，参数：{}", financeReceivablesAddNewDTO);
        // 创建FinanceReceivables对象
        FinanceReceivables financeReceivables = new FinanceReceivables();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到FinanceReceivables对象中
        BeanUtils.copyProperties(financeReceivablesAddNewDTO, financeReceivables);
        // 调用Mapper对象的insert()执行插入应收帐数据
        log.debug("即将执行插入数据，参数：{}", financeReceivables);
        financeReceivablesMapper.insert(financeReceivables);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除应收帐】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        FinanceReceivablesStandardVO financeReceivables = financeReceivablesMapper.getStandardById(id);
        log.debug("根据ID={}检查应收帐数据是否存在，查询结果：{}", id, financeReceivables);
        // 判断查询结果是否为null
        if (financeReceivables == null) {
            // 是：数据不存在，抛出异常
            String message = "删除应收帐失败，尝试删除的应收帐数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        financeReceivablesMapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, FinanceReceivablesUpdateDTO financeReceivablesUpdateDTO) {
        log.debug("开始处理【修改应收帐详情】的业务，ID：{}，新数据：{}", id, financeReceivablesUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        FinanceReceivablesStandardVO queryResult = financeReceivablesMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改应收帐详情失败，尝试修改的应收帐数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 创建FinanceReceivables对象
        FinanceReceivables financeReceivables = new FinanceReceivables();
        // 复制属性，设置ID
        BeanUtils.copyProperties(financeReceivablesUpdateDTO, financeReceivables);
        financeReceivables.setId(id);
        // 调用Mapper对象的update()方法执行修改
        financeReceivablesMapper.update(financeReceivables);
    }

    @Override
    public FinanceReceivablesStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询应收帐详情】的业务，参数：{}", id);
        // FinanceReceivablesStandardVO queryResult = financeReceivablesMapper.getStandardById(id);
        FinanceReceivablesStandardVO queryResult = financeReceivablesMapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询应收帐详情失败，应收帐数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<FinanceReceivablesListVO> list() {
        log.debug("开始处理【查询应收帐列表】的业务，无参数");
        // List<FinanceReceivablesListVO> list = financeReceivablesMapper.list();
        List<FinanceReceivablesListVO> list = financeReceivablesMapper.list();
        return list;
    }

}
